package org.aj.common.web.file.config;


import org.aj.common.web.file.service.DefaultFileCheckServiceImpl;
import org.aj.common.web.file.service.FileCheckService;
import org.aj.common.web.file.service.FileUploadBaseServiceImpl;
import org.aj.common.web.file.web.FileUploadBaseController;
import org.aj.distribute.file.client.DistributeFileClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jcj
 * @description自动配置类
 * date  2021/4/11 21:49
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(value = {DistributeFileClientProperties.class,DistributeFileServerProperties.class
        ,FileCheckRuleProperties.class})
public class CustomAutoConfiguration {



    @Bean(name = "distributeFileClient")
    DistributeFileClient distributeFileClient(DistributeFileServerProperties distributeFileServerProperties, DistributeFileClientProperties distributeFileClientProperties){
        DistributeFileClient distributeFileClient = new DistributeFileClient(distributeFileServerProperties.getHost()
                , distributeFileServerProperties.getSsl()
                , distributeFileServerProperties.getPort()
                , distributeFileServerProperties.getUsername()
                , distributeFileServerProperties.getPassword()
                , distributeFileServerProperties.getDir()
                , distributeFileClientProperties.getHttpClientConfig());
        if(StringUtils.isNotBlank(distributeFileServerProperties.getUrlRoot())){
            distributeFileClient.setUrlRoot(distributeFileServerProperties.getUrlRoot());
        }
        return distributeFileClient;

    }


    @Bean
    @ConditionalOnMissingBean(value = FileCheckService.class)
    FileCheckService fileCheckService(FileCheckRuleProperties fileCheckRuleProperties){
        return new DefaultFileCheckServiceImpl(fileCheckRuleProperties.getCheckFileRuleList());
    }

    @Bean
    public FileUploadBaseServiceImpl fileUploadBaseService(DistributeFileServerProperties distributeFileServerProperties
            ,DistributeFileClientProperties distributeFileClientProperties,FileCheckService fileCheckService){

        return new FileUploadBaseServiceImpl(distributeFileClient(distributeFileServerProperties
                ,distributeFileClientProperties),fileCheckService);
    }

    @Bean
    public FileUploadBaseController fileUploadBaseController(DistributeFileServerProperties distributeFileServerProperties
            ,DistributeFileClientProperties distributeFileClientProperties,FileCheckService fileCheckService){

        FileUploadBaseController fileUploadBaseController = new FileUploadBaseController(fileUploadBaseService(distributeFileServerProperties,distributeFileClientProperties,fileCheckService));


        return fileUploadBaseController;
    }





}
